﻿//*****************************************************************************
//
//  Silver Games - Checkers
//  Copyright © 2009 Randi Relander
//      <rjrelander@users.sourceforge.net>
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
//*****************************************************************************

namespace Checkers.Common
{
    public class Board
    {
        Tile[,] mTiles = new Tile[8, 8];

        public Board()
        {
            for (int x = 0; x < 8; x++)
            {
                for (int y = 0; y < 8; y++)
                {
                    if ((x % 2) == (y % 2))
                    {
                        mTiles[x, y] = new Tile(this, TileColor.Black, x, y);
                    }
                    else
                    {
                        mTiles[x, y] = new Tile(this, TileColor.Red, x, y);
                    }
                }
            }
        }

        public Tile GetTile(int x, int y)
        {
            if ((x < 0) || (x >= 8)) return null;
            if ((y < 0) || (y >= 8)) return null;

            return mTiles[x, y];
        }

        public Tile GetNextTile(Tile tile, CheckerColor color, Direction direction, int distance)
        {
            int dx, dy;

            switch (direction)
            {
                case Direction.NorthEast:
                    dx = 1;
                    dy = 1;
                    break;
                case Direction.NorthWest:
                    dx = -1;
                    dy = 1;
                    break;
                case Direction.SouthEast:
                    dx = 1;
                    dy = -1;
                    break;
                case Direction.SouthWest:
                    dx = -1;
                    dy = -1;
                    break;
                default:
                    return null;
            }

            switch (color)
            {
                case CheckerColor.Red:

                    break;

                case CheckerColor.Black:

                    dx = -dx;
                    dy = -dy;

                    break;

                default:

                    return null;
            }

            int x = tile.X + dx * distance;
            int y = tile.Y + dy * distance;

            return GetTile(x, y);
        }
    }
}
